import rclpy
import random
import string
from rclpy.node import Node
from std_msgs.msg import String 
from university_interfaces.srv import GradeHomework


class StudentNode(Node):

    def __init__(self, name):
        super().__init__(name)
        self.subscription = self.create_subscription(String, "homework",self.subscribe_callback,10)
        self.client = self.create_client(GradeHomework,'grade_homework')
        while not self.client.wait_for_service(timeout_sec=1.0):
            self.get_logger().info('wait for server......')
        self.request = GradeHomework.Request()
        

    def subscribe_callback(self,msg):
        self.get_logger().info('the teacher says:"%s", and now i need to finish it' % msg.data)
        self.request.name = 'lisi'
        self.request.content = generate_random_string()
        self.future = self.client.call_async(self.request)


            

def main(args=None):
    rclpy.init(args=args) 
    node = StudentNode("lisi")
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()

def generate_random_string():
    # 随机生成字符串长度（0 到 100）
    length = random.randint(0, 100)
    # 定义可选字符集（字母 + 数字）
    characters = string.ascii_letters + string.digits
    # 生成随机字符串
    random_string = ''.join(random.choice(characters) for _ in range(length))
    return random_string